package cn.qn.controller.app;

import cn.qn.base.PageVO;
import cn.qn.dto.ResponseDTO;
import cn.qn.dto.user.*;
import cn.qn.exceptions.BizException;
import cn.qn.framwork.security.MemberAccountUtils;
import cn.qn.moudle.user.AppUser;
import cn.qn.service.user.AppUserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Bryant
 * @date: 2019/5/29
 * @description:
 */
@RestController
@RequestMapping("/app/user")
public class AppUserController {

    @Reference(check = false,timeout = 30000)
    private AppUserService appUserService;

    @RequestMapping(value = "/list" ,name="app用户列表接口",method = RequestMethod.GET)
    public ResponseDTO list( AppUserQueryDTO query){
        PageVO<AppUserDTO> appUsers = appUserService.pageQueryAppUsers(query);
        return ResponseDTO.success(appUsers);
    }

    @RequestMapping(value = "/detail/{userId}" ,name="app用户详情接口",method = RequestMethod.GET)
    public ResponseDTO detail(@PathVariable(value = "userId") String userId){
        AppUserDTO appUser = appUserService.queryDetailById(userId);
        return ResponseDTO.success(appUser);
    }

    @RequestMapping(value = "/edit/status" ,name="修改用户状态接口",method = RequestMethod.POST)
    public ResponseDTO editUserStatus(@RequestBody AppUserDTO dto){
        try {
            dto.setUpdateBy(MemberAccountUtils.getCurrentMember().getUsername());
            appUserService.editStatus(dto);
            return ResponseDTO.success();
        }catch (BizException e){
            return ResponseDTO.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/add" ,name="添加平台用户",method = RequestMethod.POST)
    public ResponseDTO addPlatAppUser(@RequestBody AppUserAddDTO dto){
        try {
            dto.setCreateBy(MemberAccountUtils.getCurrentMember().getUsername());
            appUserService.addPlatAppUser(dto);
            return ResponseDTO.success();
        }catch (BizException e){
            return ResponseDTO.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/edit" ,name="修改平台用户",method = RequestMethod.POST)
    public ResponseDTO editPlatAppUser(@RequestBody AppUserEditDTO dto){
        try {
            dto.setUpdateBy(MemberAccountUtils.getCurrentMember().getUsername());
            appUserService.editPlatAppUser(dto);
            return ResponseDTO.success();
        }catch (BizException e){
            return ResponseDTO.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/resetPass" ,name="重置平台用户密码",method = RequestMethod.POST)
    public ResponseDTO resetPassword(@RequestBody AppUserResetPwdDTO dto){
        try {
            dto.setUpdateBy(MemberAccountUtils.getCurrentMember().getUsername());
            appUserService.resetPassword(dto);
            return ResponseDTO.success();
        }catch (BizException e){
            return ResponseDTO.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/plat/list" ,name="app平台用户列表接口",method = RequestMethod.GET)
    public ResponseDTO platUseList(){
        List<AppUser> appUsers = appUserService.queryPlatUserList();
        return ResponseDTO.success(appUsers);
    }

}
